<?php
/**
 * Created by PhpStorm.
 * User: Edik
 * Date: 2016/3/4
 * Time: 10:23
 */

namespace Admin\Controller;


use Admin\Model\ServerModel;

class ServerController extends AdminBaseController
{
    function lst(){
        $model = M();

        $data = $model->query("select r.id,r.title,e.`name`,r.home_img,m.nickname,r.create_time from ed_server r left join ed_server_type e on r.server_type_id = e.id left join ed_member m on r.user_id = m.id order by id desc");

        $this->assign("server", $data);
        $this->display();
    }

    function add(){
        if(IS_POST){

            $model = new ServerModel();
            if($model->create(I('post.'),1)){
                if($id = $model->add()){
                    $this->success("添加成功！",U('lst'));
                    exit;
                }
            }else{
                $this->error($model->getError());
            }
        }

        $model = M();
        $data = $model->query("select * from ed_server_type order by sort");

        $this->assign("st",$data);
        $this->display();
    }

    function delete(){
        $model = new ServerModel();
        $server = $model->find(I('get.id'));
        if($model->delete(I('get.id'),0) != FALSE){
            deleteImage(array($server['home_img']));
            $this->success("删除成功！",U('lst'));
            exit;
        }else{
            $this->error($model->getError());
        }
    }

    function edit(){
        $model = new ServerModel();
        if(IS_POST){
            if($model->create(I('post.'), 2))
            {
                if($model->save() !== FALSE)
                {
                    $this->success('修改成功！', U('lst'));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        $modelt = M();
        $datas = $modelt->query("select * from ed_server_type order by sort");

        $this->assign("st",$datas);

        $this->assign("s",$model->find(I('get.id')));
        $this->display();
    }
}